package com.sphd.beautysalon.activitys.funding;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.beautysalon.R;
import com.sphd.beautysalon.base.BaseActivity;
import com.sphd.beautysalon.base.RestaurantUrl;
import com.sphd.beautysalon.info.InvestRuleInfo;
import com.sphd.beautysalon.info.RestaurantInfo;
import com.sphd.beautysalon.presenter.SetFudingPresenterImpl;
import com.sphd.beautysalon.utils.Constant;
import com.sphd.beautysalon.view.SetFudingView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetFundingActivity extends BaseActivity implements SetFudingView {
    Intent intent;
    Bundle bundle;
    int Type = 0; //w为0时表示未经过设置 为1时候表示查看或修改
    boolean isAllClose;
    InvestRuleInfo investRuleInfo;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.m_account_money)
    MaterialEditText mAccountMoney;
    @BindView(R.id.m_account_permillage)
    MaterialEditText mAccountPermillage;
    @BindView(R.id.m_days)
    MaterialEditText mDays;
    @BindView(R.id.btn_over)
    Button btnOver;
    @BindView(R.id.content_set_funding)
    LinearLayout contentSetFunding;
    private RestaurantInfo restaurantInfo;
    private SetFudingPresenterImpl setFudingPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_funding);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        setTitleBar(toolbar, "投资设置");
        intent = getIntent();
        bundle = intent.getExtras();
        setFudingPresenter = new SetFudingPresenterImpl(this);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        Type = bundle.getInt(Constant.TYPE);
        if (Type == 1) {
            isAllClose = bundle.getBoolean(Constant.CLOSEALL);
            investRuleInfo = (InvestRuleInfo) bundle.getSerializable(Constant.INVESTRULE);
            mAccountMoney.setText(turn(investRuleInfo.getAccount_money()));
            mAccountPermillage.setText(String.valueOf(investRuleInfo.getAccount_permillage()));
            mDays.setText(String.valueOf(investRuleInfo.getAccount_floor_day()));
            if (!isAllClose) {
                btnOver.setVisibility(View.GONE);
                toast("在有账户开启的状态下不可调整设置");
            }
        }

    }

    @OnClick(R.id.btn_over)
    public void onClick() {
        if (restaurantInfo.getId() == 0) {
            toast("未能获取有效的餐馆信息");
        } else {
            if (TextUtils.isEmpty(mAccountMoney.getText().toString()) ||
                    Double.parseDouble(mAccountMoney.getText().toString()) <= 0) {
                toast("请输入合理的金额");
            } else {
                if (TextUtils.isEmpty(mAccountPermillage.getText().toString()) || (Double.parseDouble(mAccountPermillage.getText().toString())) <= 0 ||
                        (Double.parseDouble(mAccountPermillage.getText().toString())) >= 1000) {
                    toast("请输入0-1000的分成比");
                } else {
                    if (TextUtils.isEmpty(mDays.getText().toString()) || Double.parseDouble(mDays.getText().toString().trim()) <= 0 ||
                            !isNumber1(mDays.getText().toString().trim())) {
                        toast("请输入整数天数");
                    } else {
                        setFudingPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.
                                SETFUNDING + "cgid=" + restaurantInfo.getId() + "&money=" + turn(mAccountMoney.getText().toString()) + "&permillage=" +
                                turn(mAccountPermillage.getText()) + "&floor_day=" + mDays.getText());
                    }
                }
            }
        }


    }

    @Override
    public void recResSucced() {
        toast("成功");
    }

    @Override
    public void recResFaik() {
        toast("失败请重试");
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
